Session 6a: Importing data
January 20, 2023
Student-to-teacher ratios in different parts of the world:
Rows: 180
Columns: 20
$ indicator <chr> "Primary Education", "Primary Education", "Primar…
$ country <chr> "Afghanistan", "Albania", "Algeria", "Angola", "A…
$ country_code <chr> "AFG", "ALB", "DZA", "AGO", "ATG", "ARG", "ARM", …
$ edulit_ind <chr> "PTRHC_1", "PTRHC_1", "PTRHC_1", "PTRHC_1", "PTRH…
$ year <dbl> 2017, 2017, 2017, 2015, 2017, NA, NA, 2017, 2017,…
$ student_ratio <dbl> 44.00995, 17.94478, 24.22505, 50.02951, 12.05576,…
$ flag_codes <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ flags <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
$ name <chr> "Afghanistan", "Albania", "Algeria", "Angola", "A…
$ alpha.2 <chr> "AF", "AL", "DZ", "AO", "AG", "AR", "AM", "AT", "…
$ alpha.3 <chr> "AFG", "ALB", "DZA", "AGO", "ATG", "ARG", "ARM", …
$ country.code <chr> "004", "008", "012", "024", "028", "032", "051", …
$ iso_3166.2 <chr> "ISO 3166-2:AF", "ISO 3166-2:AL", "ISO 3166-2:DZ"…
$ region <chr> "Asia", "Europe", "Africa", "Africa", "North Amer…
$ sub.region <chr> "Southern Asia", "Southern Europe", "Northern Afr…
$ region.code <chr> "142", "150", "002", "002", "019", "019", "142", …
$ sub.region.code <chr> "034", "039", "015", "017", "029", "005", "145", …
$ x <dbl> 22, 15, 13, 13, 7, 6, 20, 15, 21, 4, 20, 23, 8, 1…
$ y <dbl> 8, 9, 11, 17, 4, 14, 6, 6, 7, 2, 9, 8, 6, 4, 5, 3…
$ student_ratio_region <dbl> 19.64278, 13.01069, 36.38758, 36.38758, 16.18269,…
Suggested reading
R for Data Science - Chapter 9 provides further details on and motivation for a project-based workflow.
st_ratios |>
ggplot(aes(x = region,
y = student_ratio,
fill = region)) +
geom_boxplot() +
# use a custom colour scale
scale_fill_brewer(palette = "Dark2") +
# use a custom theme
theme_bw() +
# flip x and y axes for a horizontal boxplot
coord_flip() +
labs(
title = "Student-teacher ratio per region",
x = "Region",
y = "Student-teacher ratio",
fill = "Region"
)st_ratios |>
ggplot(aes(x = region,
y = student_ratio)) +
# use a custom colour to fill all plots
# (not an aesthetic)
geom_boxplot(fill = "steelblue") +
# use another custom theme
# for this one, we need to install
# the {cowplot} package
theme_cowplot() +
# flip x and y axes for a horizontal boxplot
coord_flip() +
labs(
title = "Student-teacher ratio per region",
x = "",
y = "Student-teacher ratio",
fill = "Region"
)(If you would like extra practice: try doing the same as in 2 for the region with the lowest variability.)
colours()